package jehc.djshi.oauth.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.oauth.dao.OauthFunctionRoleDao;
import jehc.djshi.oauth.model.OauthFunctionRole;
import jehc.djshi.oauth.service.OauthResourcesRoleService;
import jehc.djshi.common.base.BaseService;
import jehc.djshi.common.util.ExceptionUtil;
import jehc.djshi.oauth.util.OauthUtil;
import jehc.djshi.oauth.model.OauthResourcesRole;
import org.springframework.stereotype.Service;
import jehc.djshi.oauth.dao.OauthResourcesRoleDao;
import javax.annotation.Resource;
/**
 * @Desc 授权中心资源对角色
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class OauthResourcesRoleServiceImpl extends BaseService implements OauthResourcesRoleService {

	@Resource
	private OauthResourcesRoleDao oauthResourcesRoleDao;

	@Resource
	private OauthFunctionRoleDao oauthFunctionRoleDao;

	@Resource
	OauthUtil oauthUtil;

	/**
	* 分页
	* @param condition 
	* @return
	*/
	public List<OauthResourcesRole> getOauthResourcesRoleListByCondition(Map<String,Object> condition){
		try{
			return oauthResourcesRoleDao.getOauthResourcesRoleListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
	
	/**
	 * 分配资源
	 * @param oauthResourcesRoleList
	 * @param oauthFunctionRoleList
	 * @param roleId
	 * @return
	 */
	public int addOauthResourcesRole(List<OauthResourcesRole> oauthResourcesRoleList, List<OauthFunctionRole> oauthFunctionRoleList, String roleId){
		int i = 0;
		try {
			if(!StringUtil.isEmpty(roleId)){
				Map<String, Object> condition = new HashMap<String, Object>();
				condition.put("roleId", roleId);
				i = oauthResourcesRoleDao.delOauthResourcesRole(condition);
				i = oauthFunctionRoleDao.delOauthFunctionRole(condition);
			}
			for(OauthResourcesRole oauthResourcesRole:oauthResourcesRoleList){
				oauthResourcesRoleDao.addOauthResourcesRole(oauthResourcesRole);
			}
			if(null != oauthFunctionRoleList && !oauthFunctionRoleList.isEmpty()){
				for(OauthFunctionRole oauthFunctionRole:oauthFunctionRoleList){
					oauthFunctionRoleDao.addOauthFunctionRole(oauthFunctionRole);
				}
			}
			oauthUtil.doTokenResources(roleId,null);//处理变更功能资源
			i = 1;
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

}
